home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / hc2_x / tcprogud.sit / TC Prog Guide / card_63445.txt < prev    next >
Text File  |  1991-02-27  |  16KB  |  507 lines

  1. -- card: 63445 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 4755
  5. -- name: 
  6.  
  7.  
  8. -- part 1 (field)
  9. -- low flags: 00
  10. -- high flags: 0007
  11. -- rect: left=30 top=78 right=296 bottom=478
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 4
  16. -- text size: 9
  17. -- style flags: 0
  18. -- line height: 12
  19. -- part name: 
  20.  
  21.  
  22. -- part contents for background part 7
  23. ----- text -----
  24. 206
  25.  
  26. -- part contents for card part 1
  27. ----- text -----
  28. /*
  29. *   FILE:     screen.c
  30. *   AUTHOR:   R. Gonzalez
  31. *   CREATED:  Aug. 5, 1990
  32. *
  33. *   screen.c contains several Mac-specific routines to supplement
  34. *   a typical stdio-type application with a graphics window.  The
  35. *   Macintosh Toolbox is a set of routines in ROM to produce the
  36. *   graphical user interface.  These Toolbox functions are defined in
  37. *   MacHeaders.  They are distinguished from my own functions by
  38. *   beginning with a capital letter.
  39. *
  40. *   Think C produces the "generic" console environment when a stdio
  41. *   function call appears before the standard Toolbox initializations.
  42. *   This appears to allow both input and output in the console window.
  43. *   The console comes to the front automatically when written to.
  44. *   This approach requires SelectWindow or BringToFront be used to
  45. *   bring the graphics window to the front.  These routines do this
  46. *   each time a line or circle is drawn, automatically reactivating
  47. *   the graphics window.  They also will do an erase_graphics()
  48. *   because the console will wash out the previous contents of the
  49. *   graphics window.
  50. */
  51.  
  52. # include   "screen.h"
  53. # include   <stdio.h>
  54. # include   <math.h>
  55.  
  56. # define   BACKGROUND   BLACK
  57.  
  58. /******************************************************************
  59. *   Transform view coordinate x to screen coordinates
  60. ******************************************************************/
  61. int   Screen::transform_x(double x)
  62. {
  63.     return (int) ((x+view_x)*(double) screen_width/view_width);
  64. }
  65.  
  66. /******************************************************************
  67. *   Transform view coordinate y to screen coordinates
  68. ******************************************************************/
  69. int   Screen::transform_y(double y)
  70. {
  71.     return (int) ((view_height-view_y-y) *
  72.            (double) screen_height/view_height);
  73. }
  74.  
  75. /******************************************************************
  76. *   Initialize screen.  Derived classes must create windows and
  77. *   initialize screen_width, screen_height instance variables
  78. *   before calling the inherited method.
  79. ******************************************************************/
  80. boolean   Screen::init(void)
  81. {
  82.     set_view(2.,2.,1.,1.);
  83.     color(WHITE);
  84.     erase_graphics();
  85.     return TRUE;
  86. }
  87.  
  88. /******************************************************************
  89. *   Bring graphics to front.  This is up to the derived class,
  90. *   which may then call inherited method to erase old graphic.
  91. ******************************************************************/
  92. void   Screen::graphics_to_front(void)
  93. {
  94.     erase_graphics();
  95. }
  96.  
  97. /******************************************************************
  98. *   Bring console to front  This is up to the derived class.
  99. ******************************************************************/
  100. void   Screen::console_to_front(void)
  101. {
  102. }
  103.  
  104. /******************************************************************
  105. *   Return screen aspect ratio: width/height
  106. ******************************************************************/
  107. double   Screen::get_aspect_ratio(void)
  108. {
  109.     return (double) screen_width/(double) screen_height;
  110. }
  111.  
  112. /******************************************************************
  113. *   Change view coordinate system for draw_line(), draw_circle(),
  114. *   etc.  This is in terms of the view width, height, and origin of
  115. *   the desired graphics window.  In terms of screen coordinates
  116. *   the graphics window uses the whole screen.
  117. ******************************************************************/
  118. void   Screen::set_view(double width,double height,
  119.        double x,double y)
  120. {
  121.     view_width = width;
  122.     view_height = height;
  123.     view_x = x;
  124.     view_y = y;
  125. }
  126.  
  127. /******************************************************************
  128. *   Turn cursor on.  This is up to the derived class.
  129. ******************************************************************/
  130. void   Screen::cursor_on(void)
  131. {
  132. }
  133.  
  134. /******************************************************************
  135. *   Turn cursor off.  This is up to the derived class.
  136. ******************************************************************/
  137. void   Screen::cursor_off(void)
  138. {
  139. }
  140.  
  141. /******************************************************************
  142. *   Sets the current drawing color.  Overriding method should
  143. *   set color of drawing pen and call inherited method.
  144. ******************************************************************/
  145. void   Screen::color(int x)
  146. {
  147.     present_color = x;
  148. }
  149.  
  150. /******************************************************************
  151. *   Sets graphics window to background color.  Up to derived class. 
  152. ******************************************************************/
  153. void   Screen::erase_graphics(void)
  154. {
  155. }
  156.  
  157. /******************************************************************
  158. *   draw_line() is used to draw lines using view coordinates.
  159. ******************************************************************/
  160. void   Screen::draw_line(double x1,double y1,double x2,double y2)
  161. {
  162.     move_to(x1,y1);
  163.     draw_to(x2,y2);
  164. }
  165.  
  166. /******************************************************************
  167. *   Move present pen position to new position using view
  168. *   coordinates.  Nothing is drawn.  Derived class should use
  169. *   transform_x() and transform_y() to convert to screen coords.
  170. ******************************************************************/
  171. void   Screen::move_to(double x,double y)
  172. {
  173. }
  174.  
  175. /******************************************************************
  176. *   Draw from present pen position to new position using view
  177. *   coordinates.  Derived class should use transform_x() and
  178. *   transform_y() to convert to screen coordinates.
  179. ******************************************************************/
  180. void   Screen::draw_to(double x,double y)
  181. {
  182. }
  183.  
  184. /******************************************************************
  185. *   draw_circle() draws a circle using view coordinates.  Derived
  186. *   class should use transform_x() and transform_y() to convert to
  187. *   screen coordinates.
  188. ******************************************************************/
  189. void   Screen::draw_circle(double x,double y,double r)
  190. {
  191. }
  192.  
  193. /******************************************************************
  194. *   fill_circle() draws a circle using view coordinates.  The
  195. *   circle is filled with the present pen color.  Derived class
  196. *   should use transform_x() and transform_y() to convert to screen
  197. *   coordinates.
  198. ******************************************************************/
  199. void   Screen::fill_circle(double x,double y,double r)
  200. {
  201. }
  202.  
  203. /******************************************************************
  204. *  mouse_button_is_down() checks whether the mouse button is down,
  205. *  returns TRUE if so, FALSE if not.  Up to derived class.  Make
  206. *  sure to override this if you use Screen::wait(), or override
  207. *  Screen::wait().  (Else you'll get an infinite "wait"!)
  208. ******************************************************************/
  209. boolean   Screen::mouse_button_is_down(void)
  210. {
  211.     return FALSE;
  212. }
  213.  
  214. /******************************************************************
  215. *   get_mouse_location() returns the mouse coordinates (via its
  216. *   arguments) in terms of the view coordinate system.  Derived
  217. *   class must get the screen coords of the mouse, and use the in-
  218. *   verse transformation of that in transform_x() and transform_y().
  219. ******************************************************************/
  220. void   Screen::get_mouse_location(double *x_ptr,double *y_ptr)
  221. {
  222. }
  223.  
  224. /******************************************************************
  225. *   Wait until button is pressed.
  226. ******************************************************************/
  227. void   Screen::wait(void)
  228. {
  229. while (!mouse_button_is_down())
  230. ;
  231. while (mouse_button_is_down())
  232. ;
  233. }
  234.  
  235. /*------------------- Mac_Screen methods -----------------------*/
  236.  
  237. # define   NIL_POINTER        0L
  238. # define   MOVE_TO_FRONT      -1L
  239. # define   REMOVE_ALL_EVENTS  0
  240. # define   TITLE              "\pGraphics"
  241. # define   VISIBLE            1
  242. # define   NO_GO_AWAY         0
  243. # define   NIL_REF_CON        NIL_POINTER
  244.  
  245. /******************************************************************
  246. *   You must call init() at the beginning of main().  Likely don't
  247. *   need all these initializations; most are taken from Mark &
  248. *   Reed's "Macintosh Programming Primer", Addison-Wesley, 1989
  249. ******************************************************************/
  250. boolean   Mac_Screen::init(void)
  251. {
  252. /*  Need a stdio function call to produce the generic console
  253. *   environment BEFORE the remaining inits:
  254. */
  255.     printf("\n");
  256.     console_window = FrontWindow();
  257.  
  258.     InitGraf(&thePort);
  259.     InitFonts();
  260.     FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
  261.  
  262. /*  Commented out InitWindows to preserve the menu bar.
  263. *   May be dangerous?
  264. */
  265. /*  InitWindows();
  266. */
  267.     InitMenus();
  268.     TEInit();
  269.     InitDialogs(NIL_POINTER);
  270.     InitCursor();
  271.     CrossCursor = GetCursor(crossCursor);
  272.     HideCursor();
  273.  
  274.     graphics_window = NewWindow(NIL_POINTER,&(screenBits.bounds),
  275.       TITLE,VISIBLE,plainDBox,MOVE_TO_FRONT,NO_GO_AWAY,NIL_REF_CON);
  276.     SetPort(graphics_window);
  277.     screen_width = graphics_window->portRect.right -
  278.     graphics_window->portRect.left;
  279.     screen_height = graphics_window->portRect.bottom -
  280.     graphics_window->portRect.top;
  281.     return inherited::init();
  282. }
  283.  
  284. /******************************************************************
  285. *   Bring graphics to front.  Never needed with Mac version,
  286. *   since this is done by the drawing routines.
  287. ******************************************************************/
  288. void   Mac_Screen::graphics_to_front(void)
  289. {
  290.     SelectWindow(graphics_window);
  291.     inherited::graphics_to_front();
  292. }
  293.  
  294. /******************************************************************
  295. *   Bring console to front.  Never needed with Mac version, since
  296. *   this happens automatically when stdio is used.
  297. ******************************************************************/
  298. void   Mac_Screen::console_to_front(void)
  299. {
  300.     SelectWindow(console_window);
  301. }
  302.  
  303. /******************************************************************
  304. *   Turn cursor on 
  305. ******************************************************************/
  306. void   Mac_Screen::cursor_on(void)
  307. {
  308.     SetCursor(*CrossCursor);
  309.     ShowCursor();
  310. }
  311.  
  312. /******************************************************************
  313. *   Turn cursor off 
  314. ******************************************************************/
  315. void   Mac_Screen::cursor_off(void)
  316. {
  317.     HideCursor();
  318. }
  319.  
  320. /******************************************************************
  321. *   color() sets the current drawing color.  The colors are defined
  322. *   in Macheaders.
  323. ******************************************************************/
  324. void   Mac_Screen::color(int x)
  325. {
  326.     inherited::color(x);
  327.  
  328.     switch (x)
  329.     {
  330.         case 0:    ForeColor(blackColor);
  331.                    break;
  332.         case 1:    ForeColor(whiteColor);
  333.                    break;
  334.         case 2:    ForeColor(redColor);
  335.                    break;
  336.         case 3:    ForeColor(yellowColor);
  337.                    break;
  338.         case 4:    ForeColor(greenColor);
  339.                    break;
  340.         case 5:    ForeColor(blueColor);
  341.                    break;
  342.         case 6:    ForeColor(cyanColor);
  343.                    break;
  344.         case 7:    ForeColor(magentaColor);
  345.                    break;
  346.         default:   break;
  347.     }
  348. }
  349.  
  350. /******************************************************************
  351. *   erase_graphics() calls the appropriate Mac Toolbox function to
  352. *   make the entire screen the background color. 
  353. ******************************************************************/
  354. void   Mac_Screen::erase_graphics(void)
  355. {
  356.     int   temp_color;
  357.  
  358.     if (graphics_window != FrontWindow())
  359.     SelectWindow(graphics_window);
  360.  
  361.     temp_color = present_color;
  362.     color(BACKGROUND);
  363.     FillRect(&(graphics_window->portRect),black);
  364.     color(temp_color);
  365. }
  366.  
  367. /******************************************************************
  368. *   Move present pen position to new position using view
  369. *   coordinates.  After transformation into screen coordinates the
  370. *   Macintosh QuickDraw routines are called.
  371. ******************************************************************/
  372. void   Mac_Screen::move_to(double x,double y)
  373. {
  374.     int   screen_x,
  375.           screen_y;
  376.  
  377.     if (graphics_window != FrontWindow())
  378.     {
  379.         SelectWindow(graphics_window);
  380.         erase_graphics();
  381.     }
  382.  
  383.     screen_x = transform_x(x);
  384.     screen_y = transform_y(y);
  385.  
  386.     MoveTo(screen_x,screen_y);
  387. }
  388.  
  389. /******************************************************************
  390. *   Draw from present pen position to new position using view
  391. *   coordinates.  After transformation into screen coordinates the
  392. *   Macintosh QuickDraw routines are called.
  393. ******************************************************************/
  394. void   Mac_Screen::draw_to(double x,double y)
  395. {
  396.     int   screen_x,
  397.           screen_y;
  398.  
  399.     if (graphics_window != FrontWindow())
  400.     {
  401.         SelectWindow(graphics_window);
  402.         erase_graphics();
  403.     }
  404.  
  405.     screen_x = transform_x(x);
  406.     screen_y = transform_y(y);
  407.  
  408.     LineTo(screen_x,screen_y);
  409. }
  410.  
  411. /******************************************************************
  412. *   draw_circle() draws a circle using the Mac Toolbox routines.  It
  413. *   accepts view coordinates.
  414. ******************************************************************/
  415. void   Mac_Screen::draw_circle(double x,double y,double r)
  416. {
  417.     Rect   myRect,
  418.            *myRectPtr;
  419.     int    screen_x,
  420.            screen_y,
  421.            screen_r;
  422.  
  423.     if (graphics_window != FrontWindow())
  424.     {
  425.         SelectWindow(graphics_window);
  426.         erase_graphics();
  427.     }
  428.  
  429.     screen_x = transform_x(x);
  430.     screen_y = transform_y(y);
  431.     screen_r = (int) (r*(double) screen_width/view_width);
  432.  
  433.     myRectPtr = &myRect;
  434.  
  435.     myRectPtr->left = screen_x-screen_r;
  436.     myRectPtr->right = screen_x+screen_r;
  437.     myRectPtr->top = screen_y-screen_r;
  438.     myRectPtr->bottom = screen_y+screen_r;
  439.         
  440.     FrameOval(&myRect);
  441. }
  442.  
  443. /******************************************************************
  444. *   fill_circle() draws a circle using the Mac Toolbox routines.  It
  445. *   accepts view coordinates.  The circle is filled with the present
  446. *   pen color
  447. ******************************************************************/
  448. void   Mac_Screen::fill_circle(double x,double y,double r)
  449. {
  450.     Rect   myRect,
  451.            *myRectPtr;
  452.     int    screen_x,
  453.            screen_y,
  454.            screen_r;
  455.  
  456.     if (graphics_window != FrontWindow())
  457.     {
  458.         SelectWindow(graphics_window);
  459.         erase_graphics();
  460.     }
  461.  
  462.     screen_x = transform_x(x);
  463.     screen_y = transform_y(y);
  464.     screen_r = (int) (r*(double) screen_width/view_width);
  465.  
  466.     myRectPtr = &myRect;
  467.  
  468.     myRectPtr->left = screen_x-screen_r;
  469.     myRectPtr->right = screen_x+screen_r;
  470.     myRectPtr->top = screen_y-screen_r;
  471.     myRectPtr->bottom = screen_y+screen_r;
  472.  
  473.     PaintOval(&myRect);
  474. }
  475.  
  476. /******************************************************************
  477. *   mouse_button_is_down() checks whether the mouse button is down,
  478. *   returns TRUE if so, FALSE if not.
  479. ******************************************************************/
  480. boolean   Mac_Screen::mouse_button_is_down(void)
  481. {
  482.     return Button();
  483. }
  484.  
  485. /******************************************************************
  486. *   get_mouse_location() returns the mouse coordinates in terms of
  487. *   the view coordinate system, using the appropriate Mac Toolbox
  488. *   functions and the inverse transformation of that in
  489. *   transform_x() and transform_y().
  490. ******************************************************************/
  491. void   Mac_Screen::get_mouse_location(double *x_ptr,double *y_ptr)
  492. {
  493.     Point   mouseLoc;
  494.  
  495.     GetMouse(&mouseLoc);
  496.     *x_ptr = (double) mouseLoc.h/
  497.              (double) screen_width*view_width-view_x;
  498.     *y_ptr = view_height-view_y-view_height*
  499.     (double) mouseLoc.v/(double) screen_height;
  500. }
  501.  
  502.  
  503.  
  504.  
  505. -- part contents for background part 4
  506. ----- text -----
  507. File 2 of 3.  Method definitions: